package com.example.utils;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ResponseUtil {

    public static void sendSuccessResponse(HttpServletResponse response, Integer status, Object data) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        Map<String, Object> responseData = new HashMap<>();
        responseData.put("success", true);
        responseData.put("status", status);
        responseData.put("data", data);
        response.getWriter().write(JSON.toJSONString(responseData));
    }

    public static void sendErrorResponse(HttpServletResponse response,Integer status, String message) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        Map<String, Object> responseData = new HashMap<>();
        responseData.put("success", false);
        responseData.put("status", status);
        responseData.put("message", message);
        response.getWriter().write(JSON.toJSONString(responseData));
    }
}
